package app.champaign;

import java.util.HashMap;
import java.util.Map;

import org.xml.sax.HandlerBase;

public class RomanToInt{

    public static void main(String[] args) {
        System.out.println(romanToInt("III"));
    }

    public static int romanToInt(String s) {
        int result = 0;
        Map<Character, Integer> romanNumMap = new HashMap<>();
        romanNumMap.put('I', 1);
        romanNumMap.put('V', 5);
        romanNumMap.put('X', 10);
        romanNumMap.put('L', 50);
        romanNumMap.put('C', 100);
        romanNumMap.put('D', 500);
        romanNumMap.put('M', 1000);
 
        int size = s.length();
        for(int i = 0; i < size - 1; ++i)
        {
            //小的数字位于大的数字左边
            if(romanNumMap.get(s.charAt(i)) < romanNumMap.get(s.charAt(i + 1)))
            {
                result -= romanNumMap.get(s.charAt(i));
            }
            else
            {
                 result += romanNumMap.get(s.charAt(i));
            }
            
        }
        result += romanNumMap.get(s.charAt(size - 1));
        return result;
    }

}